package com.itheima.controller;


import com.itheima.annotation.Log;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
  @Autowired
  private ClazzService clazzService;

  @GetMapping
  public Result page(ClazzQueryParam clazzQueryParam){
    log.info("查询班级信息, {}", clazzQueryParam);
    PageResult<Clazz> pageClazz = clazzService.page(clazzQueryParam);
    return Result.success(pageClazz);
  }
  @Log
  @DeleteMapping("/{id}")
  public Result delete(@PathVariable Integer id){
    log.info("删除班级信息, {}", id);
    clazzService.deleteById(id);
    return Result.success();
  }
  @Log
  @PostMapping
  public Result add(@RequestBody Clazz clazz) {
    try {
      log.info("添加班级信息, {}", clazz);
      clazzService.add(clazz);
      return Result.success();
    } catch (Exception e) {
      log.error("添加班级信息失败, {}", clazz, e);
      return Result.error("添加班级信息失败");
    }
  }
  @GetMapping("/{id}")
  public Result select(@PathVariable Integer id){
    log.info("根据id查询班级信息, {}", id);
    Clazz clazz = clazzService.getClazzInfoById(id);
    return Result.success(clazz);
  }
  @Log
  @PutMapping
  public Result update(@RequestBody Clazz clazz){
    log.info("修改班级信息, {}", clazz);
    clazzService.update(clazz);
    return Result.success();
  }
  @GetMapping("/list")
  public Result getClazzList(){
    log.info("获取班级列表");
    List<Clazz> clazzList = clazzService.getClazzList();
    return Result.success(clazzList);
  }
}
